//
//  BSLoginView.m
//  SuoSiApp
//
//  Created by zhang on 16/4/5.
//  Copyright © 2016年 Ym. All rights reserved.
//

#import "BSLoginView.h"


@interface BSLoginView ()<UITextFieldDelegate>


@end

@implementation BSLoginView

- (instancetype)initWithFrame:(CGRect)frame{
 
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubViews];
    }
    return self;

}


- (void)addSubViews{
    
    //    背景图
    self.backGroundImageView = [[UIImageView alloc]initWithFrame:self.bounds];
    self.backGroundImageView.image = [UIImage imageNamed:@"loginBackground.jpg"];
    [self addSubview:_backGroundImageView];
    self.titleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(kMartin30, kScrenHeight / 3 , kScrenWidth - kMartin60, kMartin30 + kMartin20)];
    self.titleImageView.image = [UIImage imageNamed:@"titleImageView.png"];
    self.iconimageView = [[UIImageView alloc]initWithFrame:CGRectMake(kScrenWidth / 3 , kScrenHeight / 9, kScrenWidth / 3   , kScrenWidth / 3)];
    //    self.iconimageView.backgroundColor = BSRed;
    self.iconimageView.layer.cornerRadius = kScrenWidth / 6;
    self.iconimageView.image = [UIImage imageNamed:@"iconImage.png"];
    
    [self.backGroundImageView addSubview:_iconimageView];
    [self.backGroundImageView addSubview:_titleImageView];
    
    
    //    用户名
    self.userNameLB = [[UILabel alloc]initWithFrame:CGRectMake(kMartin60 + kMartin20, kScreenHeight /2, kMartin60 , kMartin30)];
    //    self.userNameLB.backgroundColor = BSRed;
    self.userNameLB.text = @"用户名";
    self.userNameLB.textColor = BSWhite;
    self.userNameTF = [[UITextField alloc]initWithFrame:CGRectMake(self.userNameLB.right + kMartin10, self.userNameLB.top , kScrenWidth - kMartin30 - self.userNameLB.right - kMartin30, self.userNameLB.height)];
    self.userNameTF.backgroundColor = [UIColor colorWithRed:58.0 /255 green:166.0/255 blue:153.0/225 alpha:0.1];
    self.userNameTF.delegate = self;
    self.userNameTF.borderStyle = UITextBorderStyleNone;
    self.userNameTF.placeholder = @"请输入用户名";
    
     self.userNameTF.tag = 500;
    
    
    self.userNameTF.keyboardType = UIKeyboardTypeNumberPad;
//    self.userNameTF.returnKeyType = UIReturnKeyDone;
    //    self.userNameTF.backgroundColor = BSGreen;
    self.usernameImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.userNameLB.left - kMartin50, self.userNameLB.top, _titleImageView.width, _titleImageView.width/530 * 63)];
    self.usernameImageView.image = [UIImage imageNamed:@"userName.png"];
    [self.backGroundImageView addSubview:_usernameImageView];
    
    
    
    //    密码
    self.passwordLB = [[UILabel alloc]initWithFrame:CGRectMake(self.userNameLB.left, self.userNameLB.bottom + kMartin20, self.userNameLB.width, self.userNameLB.height )];
    self.passwordLB.text = @"密码";
    self.passwordLB.font = [UIFont fontWithName:@"微软雅黑" size:30];
    self.passwordLB.textColor = BSWhite;
    
    //    self.passwordLB.backgroundColor = BSRed;
    self.passwordTF = [[UITextField alloc]initWithFrame:CGRectMake(self.userNameTF.left, self.passwordLB.top, self.userNameTF.width, self.passwordLB.height)];
    self.passwordTF.backgroundColor = [UIColor colorWithRed:58.0 /255 green:166.0/255 blue:153.0/225 alpha:0.1];
//    self.passwordTF.alpha = 0.1;
    self.passwordTF.tag = 501;
    self.passwordTF.placeholder = @"请输入密码";
    self.passwordTF.secureTextEntry = YES;
//    设置代理 用于取消键盘
    self.passwordTF.delegate = self;
    self.passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.passwordTF.borderStyle = UITextBorderStyleNone;

    //    self.passwordTF.backgroundColor = BSGreen;
    self.passwordImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.usernameImageView.left, self.passwordTF.top, self.usernameImageView.width, self.usernameImageView.width/530 * 63)];
    self.passwordImageView.image = [UIImage imageNamed:@"password.png"];
    [self.backGroundImageView addSubview:_passwordImageView];
    
    self.rememberLB = [[UILabel alloc]initWithFrame:CGRectMake(self.passwordLB.left, self.passwordLB.bottom + kMartin20, kScreenWidth / 4, kMartin30)];
    //    self.rememberLB.backgroundColor = BSGreen;
    self.rememberLB.text = @"记住密码";
    self.rememberLB.font = [UIFont systemFontOfSize:20];
    
    //    self.rememberBT = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    记住密码 按钮
    self.rememberBT = BSButton;
//    _rememberBT.backgroundColor = BSRed;
    _rememberBT.frame = CGRectMake(self.rememberLB.left - kMartin40 + 3, self.rememberLB.top + 3, self.rememberLB.height - kMartin5, self.rememberLB.height - kMartin5);
    
    [_rememberBT setBackgroundImage:[UIImage imageNamed:@"login_chk_notSec.png"] forState:(UIControlStateNormal)];
    

    
    self.forgetPasswordBT = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.forgetPasswordBT.frame = CGRectMake(self.rememberLB.right + kMartin20, self.rememberLB.top, kScreenWidth / 4 + kMartin20, self.rememberLB.height) ;
    //    self.forgetPasswordBT.backgroundColor = BSRed;
    [self.forgetPasswordBT setTitle:@"忘记密码?" forState:(UIControlStateNormal)];
    self.forgetPasswordBT.titleLabel.font = [UIFont systemFontOfSize:20];
    [self.forgetPasswordBT setTitleColor:BSYellow forState:(UIControlStateNormal)];
    
    
    //    登陆
    self.loginBT = BSButton;
    self.loginBT.frame = CGRectMake(self.rememberBT.left, self.rememberBT.bottom + kMartin20, kScreenWidth / 3, kMartin40);
    [self.loginBT setBackgroundImage:[UIImage imageNamed:@"login_login.png"] forState:(UIControlStateNormal)];
    
    [self.loginBT setTitle:@"立即登录" forState:(UIControlStateNormal)];
    [self.loginBT setTitleColor:BSOrange forState:(UIControlStateNormal)];
    //    self.loginBT.backgroundColor = BSRed;
    //    注册
    self.registerBT = BSButton;
    self.registerBT.frame = CGRectMake(self.loginBT.right + self.loginBT.left, self.rememberBT.bottom + kMartin20, kScreenWidth / 3, kMartin40);
    [self.registerBT setBackgroundImage:[UIImage imageNamed:@"login_reg.png"] forState:(UIControlStateNormal)];
    [self.registerBT setTitle:@"账号注册" forState:(UIControlStateNormal)];
    [self.registerBT setTitleColor:BSWhite forState:(UIControlStateNormal)];
    //    self.registerBT.backgroundColor = BSGreen;
    
    
    
#pragma mark
//    重新改用户的高度
    self.userNameLB.height = self.passwordImageView.height;
    self.userNameTF.height = self.passwordImageView.height;
    self.passwordLB.height = self.passwordImageView.height;
    self.passwordTF.height = self.passwordImageView.height;

    
    
    [self addSubview:_registerBT];
    [self addSubview:_loginBT];
    [self addSubview:_forgetPasswordBT];
    
    [self addSubview:_rememberBT];
    [self addSubview:_rememberLB];
    [self addSubview:_passwordLB];
    [self addSubview:_passwordTF];
    [self addSubview:_userNameTF];
    [self addSubview:_userNameLB];
}


// 回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];

    return YES;
}

//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//    
//   [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
//    
//    return [self validateNumber:string];
//}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField.tag != 506) {
        
        [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    
    return [self validateNumber:string];
}

- (void)textFieldDidChange:(UITextField *)textField
{
    if (textField ) {
        
        if (textField.tag == 500) {
            if (textField.text.length > 11) {
                textField.text = [textField.text substringToIndex:11];
            }
        } else if((textField.tag == 501)) {
            if (textField.text.length > 8) {
                textField.text = [textField.text substringToIndex:8];
            }
        }
    }
}
- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

@end
